/*************************************************************************
	> File Name: oj-261.cpp
	> Author: Wang Bo
	> Mail: 1276106221@qq.com
	> Created Time: 2021年01月20日 星期三 09时36分31秒
 ************************************************************************/
#include <iostream>
#include <vector>
#include <stack>
#include <climits>
using namespace std;

class Data {
public:
    Data() {
        sum = vector<long long>(max_n, 0);
        ans = vector<long long>(max_n, 0);
        ans[0] = INT64_MIN;
    }
    void lshift() {
        if (s1.empty()) return;
        s2.push(s1.top());
        erase();
    }
    void rshift() {
        if (s2.empty()) return;
        insert(s2.top());
        s2.pop();
    }
    void erase() {
        if (s1.empty()) return;
        s1.pop();
    }
    void insert(long long v) {
        s1.push(v);
        ind = s1.size();
        long long val = s1.top() + sum[ind - 1];
        long long lav = max(ans[ind - 1], val);
        sum[ind] = val;
        ans[ind] = lav;
    }
    long long query(int k) { 
        return ans[k];
    }
private:
    const int max_n = 1005;
    int ind;
    stack<long long> s1, s2;
    vector<long long> sum, ans;
};

int main() {
    Data data;
    int n, val;
    string op;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> op;
        switch (op[0]) {
            case 'I': cin >> val; data.insert(val); break;
            case 'D': data.erase(); break;
            case 'L': data.lshift(); break;
            case 'R': data.rshift(); break;
            case 'Q': cin >> val; cout << data.query(val) << endl;
        }
    }
    return 0;
}
